# encoding: utf-8
from flask import g
from wtforms import StringField, IntegerField
from ..forms import BaseForm
from wtforms.validators import InputRequired, Email, Length, EqualTo, ValidationError
from utils import bbscache


class LoginForm(BaseForm):
    email = StringField(validators=[Email(message='请填写正确邮箱格式'), InputRequired()])
    password = StringField(validators=[Length(6, 20, message='密码长度在6-20之间'), InputRequired()])
    remember = IntegerField(validators=[])


class ResetForm(BaseForm):
    oldpwd = StringField(validators=[Length(6, 20, message='原密码格式错误'), InputRequired()])
    newpwd = StringField(validators=[Length(6, 20, message='新密码格式错误'), InputRequired()])
    newpwd2 = StringField(validators=[EqualTo('newpwd', message='两次密码不一致！')])

    def validate_newpwd(self, field):
        newpwd = field.data
        if newpwd == self.oldpwd.data:
            raise ValidationError('新密码不能和原密码重复！')


class ResetEmailForm(BaseForm):
    email = StringField(validators=[Email('邮箱格式错误！')])
    captcha = StringField(validators=[Length(min=6, max=6, message='请输入正确格式的验证码！')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_cache = bbscache.get(email)
        if not captcha_cache or captcha_cache.lower() != captcha.lower():
            raise ValidationError('邮箱验证码错误！')

    def validate_email(self, field):
        email = field.data
        if email == g.cms_user.email:
            raise ValidationError('不能和原邮箱重复！')


class C_FoundPwdForm(BaseForm):
    email = StringField(validators=[Email('邮箱格式错误！')])
    captcha = StringField(validators=[Length(min=6, max=6, message='请输入正确格式的验证码！')])
    password = StringField(validators=[Length(min=6, max=20, message='密码格式错误！')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_cache = bbscache.get(email)
        if not captcha_cache or captcha_cache.lower() != captcha.lower():
            raise ValidationError('邮箱验证码错误！')


class AddBannerForm(BaseForm):
    name = StringField(validators=[InputRequired(message='输入轮播图名称！')])
    image_url = StringField(validators=[InputRequired(message='输入轮播图图片链接地址！')])
    link_url = StringField(validators=[InputRequired(message='输入轮播图跳转链接！')])
    priority = IntegerField(validators=[InputRequired(message='请输入轮播图优先级！')])


class UpdateBannerForm(AddBannerForm):
    banner_id = IntegerField(validators=[InputRequired(message='请指定轮播图的id！')])


class AddBoardForm(BaseForm):
    name = StringField(validators=[InputRequired('请输入板块名称！'), Length(max=30, message='板块名称过长！')])
    desc = StringField(validators=[InputRequired('请输入板块描述信息！'), Length(max=100, message='板块描述信息过长！')])


class UpdateBoardForm(AddBoardForm):
    board_id = IntegerField(validators=[InputRequired(message='请指定板块的id！')])


# 添加cms用户的表单验证
class AddCuserForm(BaseForm):
    username = StringField(validators=[InputRequired('用户名不能为空！'), Length(max=50, message='姓名超过限制长度！')])
    email = StringField(validators=[Email(message='请填写正确邮箱格式'), InputRequired()])
    password = StringField(validators=[Length(6, 20, message='密码长度在6-20之间'), InputRequired()])
    role_id = IntegerField(validators=[InputRequired('必须传入权限角色id！')])
